﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data; 
using System.Reflection;
using CP.DBService;
using Oracle.ManagedDataAccess.Client;


namespace GPM
{
    public class LogHelper
    {
        public class LogInfo
        {
            public string op_id { get; set; }
            public string op_name { get; set; }
            public string op_page { get; set; }
            public string op_type { get; set; }
            public string op_desc { get; set; }
            public string op_key1 { get; set; }
            public string op_key2 { get; set; }
            public string reserved { get; set; }

            public LogInfo()
            {
                op_id = string.Empty;
                op_name = string.Empty;
                op_page = string.Empty;
                op_type = string.Empty;
                op_desc = string.Empty;
                op_key1 = string.Empty;
                op_key2 = string.Empty;
                reserved = string.Empty;
            }
        }
        //记录操作log
        public static void Log(string page, string type, string desc, string key1, string key2)
        {
            try
            {
                LogInfo loginfo = new LogInfo();
                loginfo.op_id = AppSetter.CurrentUser.user_id;
                loginfo.op_name = AppSetter.CurrentUser.user_name;
                loginfo.op_page = page;
                loginfo.op_type = type;
                loginfo.op_desc = desc;
                loginfo.op_key1 = key1;
                loginfo.op_key2 = key2;
                OperateLog(loginfo);
            }
            catch (Exception e)
            {
            }
        }
        public static void Log(string page, string type, string desc, string key1)
        {
            Log(page, type, desc, key1, " ");
        }
        public static void OperateLog(LogInfo loginfo)
        {
            DBHelper db = DBHelper.CreateInstance();
            const string Ins_OperateLog = "INSERT INTO operate_log(op_id, op_name, op_page, op_type, op_desc, op_key1, op_key2, reserved) " +
                                          " VALUES(:op_id, :op_name, :op_page, :op_type, :op_desc, :op_key1, :op_key2, :reserved)";
            const string Parm_op_id = ":op_id";
            const string Parm_op_name = ":op_name";
            const string Parm_op_page = ":op_page";
            const string Parm_op_type = ":op_type";
            const string Parm_op_desc = ":op_desc";
            const string Parm_op_key1 = ":op_key1";
            const string Parm_op_key2 = ":op_key2";
            const string Parm_reserved = ":reserved";

            OracleParameter[] parms = new OracleParameter[8];

            OracleParameter parm_op_id = new OracleParameter(Parm_op_id, OracleDbType.Varchar2, 20);
            OracleParameter parm_op_name = new OracleParameter(Parm_op_name, OracleDbType.Varchar2, 20);
            OracleParameter parm_op_page = new OracleParameter(Parm_op_page, OracleDbType.Varchar2, 40);
            OracleParameter parm_op_type = new OracleParameter(Parm_op_type, OracleDbType.Varchar2, 10);
            OracleParameter parm_op_desc = new OracleParameter(Parm_op_desc, OracleDbType.Varchar2, 40);
            OracleParameter parm_op_key1 = new OracleParameter(Parm_op_key1, OracleDbType.Varchar2, 80);
            OracleParameter parm_op_key2 = new OracleParameter(Parm_op_key2, OracleDbType.Varchar2, 80);
            OracleParameter parm_reserved = new OracleParameter(Parm_reserved, OracleDbType.Varchar2, 100);

            parm_op_id.Value = AppSetter.CurrentUser.user_id;
            parm_op_name.Value = AppSetter.CurrentUser.user_name;
            parm_op_page.Value = loginfo.op_page;
            parm_op_type.Value = loginfo.op_type;
            parm_op_desc.Value = loginfo.op_desc;
            parm_op_key1.Value = loginfo.op_key1;
            parm_op_key2.Value = loginfo.op_key2;
            parm_reserved.Value = loginfo.reserved;

            parms[0] = parm_op_id;
            parms[1] = parm_op_name;
            parms[2] = parm_op_page;
            parms[3] = parm_op_type;
            parms[4] = parm_op_desc;
            parms[5] = parm_op_key1;
            parms[6] = parm_op_key2;
            parms[7] = parm_reserved;

            db.ExecuteNonQuery(Ins_OperateLog, parms, CommandType.Text);

        }
    }
}
